package cn.snailhd.utils.test;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import cn.snailhd.utils.IoUtils;

public class Web {
	public static String requestReader(String httpUrl){
		
		
		
		
		StringBuilder sb = new StringBuilder();
		BufferedInputStream bis = null;
		try {
			
			URL url = new URL(httpUrl);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setRequestMethod("GET");
			connection.connect();
			bis = new BufferedInputStream(connection.getInputStream());
			int len;
			byte[] buff  = new byte[1024];
			String encode = null;
			while((len=bis.read(buff))>-1){
		
				if(encode == null){
					String temp = new String(buff, 0, len, "GBK");
					if(temp.indexOf("charset")!=-1){
						String ss = temp.substring(temp.indexOf("charset="));
						encode = ss.substring(8, ss.indexOf("\""));
						System.out.println(encode);
						sb.append(new String(buff,0,len,encode));
					}
				}else {
					sb.append(new String(buff,0,len,encode));
				}	
				
			}
	
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			IoUtils.close(bis);
		}
		
			
		return sb.toString();
	}
	
	public static void main(String[] args) {
		String html = requestReader("http://www.16sucai.com/");
		//System.out.println(html);
	}

}


